package lesson5;

public class Magic {

		public static void main(String[] args) {
			int[][] array = {
					{2,7,6},
					{9,5,1},
					{4,3,8}
			};
			
			//find sum of first row
			int globalSum = 0;
			boolean isMagic = true;
			
			for(int i = 0; i < array.length; i++)
			{
				int rowSum = 0;
				for(int j = 0; j < array[i].length; j++)
				{
					rowSum += array[i][j];
				}
				if(globalSum == 0)//only when getting sum of first row
					globalSum = rowSum;
				if(globalSum != rowSum)
				{
					isMagic = false;
					break;
				}
			}
			//check columns
			for(int i = 0; i < array.length; i++)
			{
				int colSum = 0;
				for(int j = 0; j < array[i].length; j++)
				{
					colSum += array[j][i];
				}
				if(globalSum != colSum)
				{
					isMagic = false;
					break;
				}
			}
			
			//check main diagonal
			int mainDiagSum = 0;
			for(int i = 0; i < array.length; i++)
			{
				for(int j = 0; j < array[i].length; j++)
				{
					if(i == j)
					{
						mainDiagSum += array[i][j];
					}
				}
			}
			if(globalSum != mainDiagSum)
			{
				isMagic = false;
			}
			//check second diagonal
			int secondDiagSum = 0;
			for(int i = 0; i < array.length; i++)
			{
				for(int j = 0; j < array[i].length; j++)
				{
					if(i+j == array.length-1)
					{
						secondDiagSum += array[i][j];
					}
				}
			}
			if(globalSum != secondDiagSum)
			{
				isMagic = false;
			}
			
			System.out.println(isMagic);
			
		}
}
